package com.atguigu.dga.governance.assess.impl.cacl;

import com.atguigu.dga.dolphinscheduler.bean.TDsTaskInstance;
import com.atguigu.dga.dolphinscheduler.service.TDsTaskInstanceService;
import com.atguigu.dga.governance.assess.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.constants.MetaConst;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component("TASK_FAILED")
public class TaskFailedAssessor extends Assessor {
    @Autowired
    TDsTaskInstanceService tDsTaskInstanceService;

    @Override
    protected void assess(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        //0.获取库名和表名组成 表的全名
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        String fullTableName = tableMetaInfo.getSchemaName() + "." + tableMetaInfo.getTableName();

        //方案一：
        //1.提取DS中任务实例执行信息  要根据库名和表名 以及 考评其实运行的任务实例  和 任务执行失败的数据
/*        List<TDsTaskInstance> tDsTaskInstancesFaildList = tDsTaskInstanceService.list(new QueryWrapper<TDsTaskInstance>().eq("name",fullTableName)
                .eq("date_format(start_time,'%Y-%m-%d')",assessParam.getAssessDate())
                .eq("state", MetaConst.TASK_STATE_FAILED)
        );*/

        List<TDsTaskInstance> tDsTaskInstanceListWithAssessDate = assessParam.getTDsTaskInstanceListWithAssessDate();


        //遍历每天任务运行实例的集合 并根据表名和任务运行状态进行过滤
        //方案二：
/*    List<TDsTaskInstance> tDsTaskInstancesFaildList =new ArrayList<>();
        for (TDsTaskInstance tDsTaskInstance : tDsTaskInstanceListWithAssessDate) {
            if (tDsTaskInstance.getName().equals(fullTableName) &&(int) (tDsTaskInstance.getState())==(MetaConst.TASK_STATE_FAILED)) {
                //将符合要求的（考评表 任务实例运行失败的）放到List集合中
                tDsTaskInstancesFaildList.add(tDsTaskInstance);
            }
        }*/

        //方案三：使用Java的流式API进行过滤
/*        List<TDsTaskInstance> tDsTaskInstancesFaildList = tDsTaskInstanceListWithAssessDate.stream().filter(tDsTaskInstance -> tDsTaskInstance.getName().equals(fullTableName) && tDsTaskInstance.getState().equals(MetaConst.TASK_STATE_FAILED)).collect(Collectors.toList());*/


        List<TDsTaskInstance> tDsTaskInstancesFaildList = tDsTaskInstanceListWithAssessDate.stream().filter(tDsTaskInstance -> tDsTaskInstance.getName().equals(fullTableName) &&(int) (tDsTaskInstance.getState())==(MetaConst.TASK_STATE_FAILED)).collect(Collectors.toList());


        //根据集合中的元素个数判断，如果集合中有数据那么证明有失败的任务
        if (tDsTaskInstancesFaildList.size() > 0) { //有报错
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            governanceAssessDetail.setAssessProblem("任务有报错");
        }
    }
}
